﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SHKF.B2C.BaseHosted;
using SHKF.B2C.BLL;
using SHKF.B2C.BLL.LM;
using SHKF.B2C.Hosted;
using SHKF.B2C.Model.Dto;
using SHKF.B2C.Model.Dto.WareHouse;
using SHKF.B2C.Model.Dto.WorkingCenter;
using SHKF.B2C.MQBase;
using SHKF.B2C.MQBase.Model;
using SHKF.B2C.MQBase.Model.Order;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SHKF.B2C.WebService.Controllers
{
    /// <summary>
    /// 更新库存
    /// </summary>
    [Route("[controller]")]
    [ApiController]
    public class WareHouseController : BaseController
    {
        private readonly WorkingCenterBLL workingCenterBLL;
        private readonly WareHouseDataBLL wareHouseDataBLL;
        private readonly MQProducers producers;
       　
        public WareHouseController(
            WorkingCenterBLL workingCenterBLL,
            WareHouseDataBLL wareHouseDataBLL,
            MQProducers producers,
            ILogger<WareHouseController> logger) : base(logger)
        {
            this.workingCenterBLL = workingCenterBLL;
            this.wareHouseDataBLL = wareHouseDataBLL;
            this.producers = producers; 
        }

       /// <summary>
        /// 向消息对列发更新库存消息, 暂时只更新有赞运库存
        /// </summary>
        /// <param name="WarehousSN"></param>
        /// <param name="ProductSN"></param>
        /// <param name="Qty"></param>
        /// <param name="Forward"></param>
        /// <param name="Rubric"></param>
        /// <param name="Summary"></param>
        /// <param name="Memo"></param>
        /// <param name="Price"></param>
        /// <returns></returns>
        [HttpGet("UpdateStock")]
        public string UpdateStock( String WarehousSN, Int64 ProductSN, Int32 Qty,
            Int16 Forward, Int16 Rubric, String Summary, String Memo)
        {
            try
            {
                //更新B2C库存
                UpErpQtyDto dto = this.workingCenterBLL.UpdateProductQty(WarehousSN, ProductSN, Qty);

                //写帐
                this.workingCenterBLL.SaveBalanceAmount(dto.ShopVSN, dto.ProductVSN,
                    WarehousSN, ProductSN, Forward, Rubric, Qty, Summary, Memo);

                UpdateStock data = new UpdateStock();
                data.WarehousSN = WarehousSN;
                data.Forward = Forward;
                data.Rubric = Rubric;
                data.Summary = Summary;
                data.Memo = Memo;

                data.Data = new List<ProductStock>();
                data.Data.Add(new ProductStock()
                { 
                    ProductSN = ProductSN,
                    Qty = dto.Qty
                });
                 
                //发送广播消息,更新有赞库存　
                data.Key = "UpdatePlat";
                this.producers.SendUpdateStockQty(data);
                string msg = $"{ProductSN}:已经向mq发送消息";
                base.WriteInformation("WareHouseController>>UpdateStock", msg);
                return msg;
            }
            catch (Exception ex)
            {
                base.WriteException($"WareHouseController>>UpdateStock:{ProductSN}", ex); 
                return ex.Message;
            }
           

        }

        [HttpGet("list")]
        public V_WareHouseDataDto List(int CompanySN, int ShopSN, string Keyword)
        {
            V_WareHouseDataDto dto = new V_WareHouseDataDto();
            try
            {
                List<V_WareHouseData> list = this.wareHouseDataBLL.Query(CompanySN, ShopSN, Keyword);
                dto.data = list;
            }
            catch (Exception ex)
            {
                dto.error = new Error() { code = -1, msg = ex.Message };
            }

            return dto;
        }
    }
}
